{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "13133c05-e931-4257-ba94-1a1a761d2f65",
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "driver = webdriver.Edge()\n",
    "driver.get(\"https://digquant.com/account\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a0e343e9-f294-4e02-9e2a-8234a42101ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import random\n",
    "from selenium.webdriver.common.by import By\n",
    "def simulate_sendkeys(label,str1):\n",
    "    label.clear()\n",
    "    time.sleep(random.random()/2)\n",
    "    for i in str1:\n",
    "        time.sleep(random.random()/2)\n",
    "        label.send_keys(i)\n",
    "    time.sleep(0.5)\n",
    "a=driver.find_element(By.XPATH,\"/html/body/div[1]/div[2]/div[1]/div/div[2]/div/div[2]/form/div[1]/div/div/div/input\")\n",
    "simulate_sendkeys(a,\"15113096166\")\n",
    "b=driver.find_element(By.XPATH,\"/html/body/div[1]/div[2]/div[1]/div/div[2]/div/div[2]/form/div[2]/div/div/div/input\")\n",
    "simulate_sendkeys(b,\"DigQuant@01\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0183ef97-44d3-4c4a-a373-09ca0808a1a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.回车登录\n",
    "from selenium.webdriver.common.keys import Keys\n",
    "b.send_keys(Keys.ENTER)\n",
    "time.sleep(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3213944d-753d-4f82-a9ec-403277849187",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.鼠标点击课程页面\n",
    "from selenium.webdriver import ActionChains\n",
    "c=driver.find_element(By.XPATH,\"/html/body/div[1]/div[1]/div/div/div[2]\")\n",
    "xxx=ActionChains(driver)\n",
    "xxx.click(c).perform()\n",
    "time.sleep(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5cb4cdb3-a7f6-4c81-810f-c8dbdfe168b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "t=driver.find_element(By.XPATH,\"/html/body/div[1]/div[2]/div[1]/div[2]\")\n",
    "n = len(t.text.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "374f144b-9992-407c-baf6-5917d9c81431",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3.获取文本\n",
    "text=[]\n",
    "for n in range(1,n+1):\n",
    "    t=driver.find_element(By.XPATH,f\"/html/body/div[1]/div[2]/div[1]/div[3]/div[{n}]/div\")\n",
    "    text.append(t.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "df99b75a-5717-407e-8b86-37c405d32de6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>课程</th>\n",
       "      <th>视频</th>\n",
       "      <th>学时</th>\n",
       "      <th>购买人数</th>\n",
       "      <th>价格(￥)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Python编程基础</td>\n",
       "      <td>【2022点宽就业指导宣讲】简历写作三部曲</td>\n",
       "      <td>1.0</td>\n",
       "      <td>602</td>\n",
       "      <td>19.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Python编程基础</td>\n",
       "      <td>【2022点宽就业指导宣讲】职场新人的必备战略</td>\n",
       "      <td>1.0</td>\n",
       "      <td>366</td>\n",
       "      <td>19.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Python编程基础</td>\n",
       "      <td>【Python编程零基础】合集</td>\n",
       "      <td>12.6</td>\n",
       "      <td>183</td>\n",
       "      <td>99.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Python编程基础</td>\n",
       "      <td>【Python编程基础】（合集）</td>\n",
       "      <td>12.7</td>\n",
       "      <td>1213</td>\n",
       "      <td>99.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Python编程基础</td>\n",
       "      <td>Pandas数据分析（合集）</td>\n",
       "      <td>0.0</td>\n",
       "      <td>845</td>\n",
       "      <td>108.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>大数据分析</td>\n",
       "      <td>数据分析实战（数据分析+爬虫+数据挖掘）合集</td>\n",
       "      <td>26.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1980.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>大数据分析</td>\n",
       "      <td>【大数据分析】（合集）</td>\n",
       "      <td>17.9</td>\n",
       "      <td>20</td>\n",
       "      <td>899.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>大数据分析</td>\n",
       "      <td>【大数据分析】数据挖掘</td>\n",
       "      <td>9.6</td>\n",
       "      <td>206</td>\n",
       "      <td>399.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>大数据分析</td>\n",
       "      <td>【大数据分析】金融数据处理与可视化</td>\n",
       "      <td>8.3</td>\n",
       "      <td>206</td>\n",
       "      <td>299.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>金融人工智能</td>\n",
       "      <td>金融人工智能实践（机器学习实践）合集</td>\n",
       "      <td>16.8</td>\n",
       "      <td>3</td>\n",
       "      <td>2480.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>金融人工智能</td>\n",
       "      <td>金融人工智能实践（机器学习理论）合集</td>\n",
       "      <td>26.7</td>\n",
       "      <td>2</td>\n",
       "      <td>2480.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>金融量化投资</td>\n",
       "      <td>【金融量化投资】CTA方向+因子挖掘方向（合集）</td>\n",
       "      <td>32.1</td>\n",
       "      <td>306</td>\n",
       "      <td>3480.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>金融量化投资</td>\n",
       "      <td>【技术指标】合集</td>\n",
       "      <td>16.6</td>\n",
       "      <td>5</td>\n",
       "      <td>3480.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>金融量化投资</td>\n",
       "      <td>【单因子分析】（合集）</td>\n",
       "      <td>28.5</td>\n",
       "      <td>3</td>\n",
       "      <td>3480.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>金融量化投资</td>\n",
       "      <td>【CTA】金融量化投资分析实战（CTA量化方向）合集</td>\n",
       "      <td>15.7</td>\n",
       "      <td>3</td>\n",
       "      <td>2980.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>金融量化投资</td>\n",
       "      <td>【金融量化投资】第一课：金融量化基础与数据提取</td>\n",
       "      <td>0.0</td>\n",
       "      <td>764</td>\n",
       "      <td>99.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>信贷风控</td>\n",
       "      <td>【信贷风控】（合集）</td>\n",
       "      <td>14.5</td>\n",
       "      <td>477</td>\n",
       "      <td>1580.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>信贷风控</td>\n",
       "      <td>【信贷风控】信用评分模型案例实战</td>\n",
       "      <td>5.1</td>\n",
       "      <td>432</td>\n",
       "      <td>499.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>信贷风控</td>\n",
       "      <td>【信贷风控】信贷风控与信用评分模型</td>\n",
       "      <td>6.0</td>\n",
       "      <td>322</td>\n",
       "      <td>399.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>信贷风控</td>\n",
       "      <td>【信贷风控】消费金融与数据分析初识</td>\n",
       "      <td>3.5</td>\n",
       "      <td>302</td>\n",
       "      <td>69.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>财富管理</td>\n",
       "      <td>【投资组合风险配置】绿色金融与新能源产业</td>\n",
       "      <td>1.2</td>\n",
       "      <td>3</td>\n",
       "      <td>99.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>财富管理</td>\n",
       "      <td>【财富管理--资产配置】(合集)</td>\n",
       "      <td>16.6</td>\n",
       "      <td>1</td>\n",
       "      <td>2480.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>财富管理</td>\n",
       "      <td>【财富管理--资产配置】 经典投资模型</td>\n",
       "      <td>5.1</td>\n",
       "      <td>549</td>\n",
       "      <td>399.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>财富管理</td>\n",
       "      <td>【财富管理--资产配置】资产配置理论</td>\n",
       "      <td>4.3</td>\n",
       "      <td>352</td>\n",
       "      <td>899.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>财富管理</td>\n",
       "      <td>【财富管理--资产配置】基金基础知识</td>\n",
       "      <td>5.8</td>\n",
       "      <td>2</td>\n",
       "      <td>299.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>区块链</td>\n",
       "      <td>【区块链】区块链金融技术原理与应用（合集）</td>\n",
       "      <td>16.1</td>\n",
       "      <td>202</td>\n",
       "      <td>2980.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>区块链</td>\n",
       "      <td>【区块链金融（通识）】合集</td>\n",
       "      <td>18.3</td>\n",
       "      <td>362</td>\n",
       "      <td>2880.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>区块链</td>\n",
       "      <td>【区块链金融（通识）】分布式账本在金融行业的实际应用</td>\n",
       "      <td>6.8</td>\n",
       "      <td>373</td>\n",
       "      <td>899.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>区块链</td>\n",
       "      <td>【区块链金融（通识）】以太坊概述以及Hyperledger Fabric和R3 Corda原理介绍</td>\n",
       "      <td>6.6</td>\n",
       "      <td>351</td>\n",
       "      <td>799.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>区块链</td>\n",
       "      <td>【区块链金融（通识）】区块链技术与分布式账本介绍</td>\n",
       "      <td>4.8</td>\n",
       "      <td>404</td>\n",
       "      <td>699.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>AIGC</td>\n",
       "      <td>AI创意实现：AIGC中文大语言模型应用与实践（合集）</td>\n",
       "      <td>17.9</td>\n",
       "      <td>247</td>\n",
       "      <td>2480.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>AIGC</td>\n",
       "      <td>基于大语言模型的智能体（AI Agent）（合集）</td>\n",
       "      <td>24.0</td>\n",
       "      <td>146</td>\n",
       "      <td>2480.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>AIGC</td>\n",
       "      <td>AI创意实现：ChatGPT与AIGC工具的高效应用（合集）</td>\n",
       "      <td>19.8</td>\n",
       "      <td>1</td>\n",
       "      <td>3280.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>AIGC</td>\n",
       "      <td>AIGC基础</td>\n",
       "      <td>2.1</td>\n",
       "      <td>0</td>\n",
       "      <td>99.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            课程                                                 视频    学时  购买人数  \\\n",
       "0   Python编程基础                              【2022点宽就业指导宣讲】简历写作三部曲   1.0   602   \n",
       "1   Python编程基础                            【2022点宽就业指导宣讲】职场新人的必备战略   1.0   366   \n",
       "2   Python编程基础                                    【Python编程零基础】合集  12.6   183   \n",
       "3   Python编程基础                                   【Python编程基础】（合集）  12.7  1213   \n",
       "4   Python编程基础                                     Pandas数据分析（合集）   0.0   845   \n",
       "5        大数据分析                             数据分析实战（数据分析+爬虫+数据挖掘）合集  26.0     4   \n",
       "6        大数据分析                                        【大数据分析】（合集）  17.9    20   \n",
       "7        大数据分析                                        【大数据分析】数据挖掘   9.6   206   \n",
       "8        大数据分析                                  【大数据分析】金融数据处理与可视化   8.3   206   \n",
       "9       金融人工智能                                 金融人工智能实践（机器学习实践）合集  16.8     3   \n",
       "10      金融人工智能                                 金融人工智能实践（机器学习理论）合集  26.7     2   \n",
       "11      金融量化投资                           【金融量化投资】CTA方向+因子挖掘方向（合集）  32.1   306   \n",
       "12      金融量化投资                                           【技术指标】合集  16.6     5   \n",
       "13      金融量化投资                                        【单因子分析】（合集）  28.5     3   \n",
       "14      金融量化投资                         【CTA】金融量化投资分析实战（CTA量化方向）合集  15.7     3   \n",
       "15      金融量化投资                            【金融量化投资】第一课：金融量化基础与数据提取   0.0   764   \n",
       "16        信贷风控                                         【信贷风控】（合集）  14.5   477   \n",
       "17        信贷风控                                   【信贷风控】信用评分模型案例实战   5.1   432   \n",
       "18        信贷风控                                  【信贷风控】信贷风控与信用评分模型   6.0   322   \n",
       "19        信贷风控                                  【信贷风控】消费金融与数据分析初识   3.5   302   \n",
       "20        财富管理                               【投资组合风险配置】绿色金融与新能源产业   1.2     3   \n",
       "21        财富管理                                   【财富管理--资产配置】(合集)  16.6     1   \n",
       "22        财富管理                                【财富管理--资产配置】 经典投资模型   5.1   549   \n",
       "23        财富管理                                 【财富管理--资产配置】资产配置理论   4.3   352   \n",
       "24        财富管理                                 【财富管理--资产配置】基金基础知识   5.8     2   \n",
       "25         区块链                              【区块链】区块链金融技术原理与应用（合集）  16.1   202   \n",
       "26         区块链                                      【区块链金融（通识）】合集  18.3   362   \n",
       "27         区块链                         【区块链金融（通识）】分布式账本在金融行业的实际应用   6.8   373   \n",
       "28         区块链  【区块链金融（通识）】以太坊概述以及Hyperledger Fabric和R3 Corda原理介绍   6.6   351   \n",
       "29         区块链                           【区块链金融（通识）】区块链技术与分布式账本介绍   4.8   404   \n",
       "30        AIGC                        AI创意实现：AIGC中文大语言模型应用与实践（合集）  17.9   247   \n",
       "31        AIGC                          基于大语言模型的智能体（AI Agent）（合集）  24.0   146   \n",
       "32        AIGC                     AI创意实现：ChatGPT与AIGC工具的高效应用（合集）  19.8     1   \n",
       "33        AIGC                                             AIGC基础   2.1     0   \n",
       "\n",
       "     价格(￥)  \n",
       "0     19.8  \n",
       "1     19.8  \n",
       "2     99.0  \n",
       "3     99.0  \n",
       "4    108.0  \n",
       "5   1980.0  \n",
       "6    899.0  \n",
       "7    399.0  \n",
       "8    299.0  \n",
       "9   2480.0  \n",
       "10  2480.0  \n",
       "11  3480.0  \n",
       "12  3480.0  \n",
       "13  3480.0  \n",
       "14  2980.0  \n",
       "15    99.0  \n",
       "16  1580.0  \n",
       "17   499.0  \n",
       "18   399.0  \n",
       "19    69.0  \n",
       "20    99.0  \n",
       "21  2480.0  \n",
       "22   399.0  \n",
       "23   899.0  \n",
       "24   299.0  \n",
       "25  2980.0  \n",
       "26  2880.0  \n",
       "27   899.0  \n",
       "28   799.0  \n",
       "29   699.0  \n",
       "30  2480.0  \n",
       "31  2480.0  \n",
       "32  3280.0  \n",
       "33    99.0  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.整理文本\n",
    "import pandas as pd\n",
    "import re\n",
    "all_courses = []\n",
    "for block in text:\n",
    "    lines = block.split('\\n')\n",
    "    category = lines[0]  # 课程类别\n",
    "    for i in range(4,len(lines)-2,3):\n",
    "            video = lines[i].strip()\n",
    "            match = re.search(r'共(\\d+\\.?\\d*)\\s*学时\\s*(\\d+)人购买', lines[i+1])\n",
    "            hours = float(match.group(1))  #学时\n",
    "            buyers = int(match.group(2))  # 购买人数\n",
    "            price_match = re.search(r'¥\\s*([\\d.]+)', lines[i+2])\n",
    "            price = float(price_match.group(1))  # 提取价格\n",
    "            all_courses.append({'课程': category,'视频': video,'学时': hours,'购买人数': buyers,'价格(￥)': price})\n",
    "df = pd.DataFrame(all_courses)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c85c94a-24a8-4343-9d03-27cbf420d48d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
